using System;
using System.Web;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Share;

namespace ASPNETVN.PORTAL.Modules.FAQ.Controls
{
    public partial class AddEditMessage : AddItemControl
    {
        Components.Item item=new ASPNETVN.PORTAL.Modules.FAQ.Components.Item();
        string oldAnswerContent = "";
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                item = Components.Item.Load(this.ItemID);
                if (item != null)
                {
                    ddlCategory.SelectedValue = item.CategoryID.ToString();
                    lblTieuDe.Text = item.Title;
                    lblNguoiGui.Text = item.Name;
                    lblEmail.Text = item.Email;
                    lblNgayGui.Text = item.QuesDate.ToString().Substring(0, item.QuesDate.ToString().IndexOf(" "));
                    lblNoiDungHoi.Text = item.QuesContent;
                    this.txtNguoiTraLoi.Text = item.AnwserBy;
                    this.txtNgayTraLoi.Text = item.AnwserDate.ToString().Substring(0, item.AnwserDate.ToString().IndexOf(" "));
                    oldAnswerContent = item.QuesContent.Trim();
                    this.txtNoiDungTraLoi.Text = item.AnwserContent;
                    this.cbStatus.SelectedValue = item.Status.ToString();
                }
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (this.ItemID != 0)
                {
                    item.ID = this.ItemID;
                    item.CategoryID = Convert.ToInt32(ddlCategory.SelectedValue);
                    item.Title = lblTieuDe.Text;
                    item.Name = lblNguoiGui.Text;
                    item.Email = lblEmail.Text;
                    item.QuesDate = Convert.ToDateTime(lblNgayGui.Text);
                    item.QuesContent = lblNoiDungHoi.Text;
                    if (this.txtNoiDungTraLoi.Text.Trim() != "")
                    {
                        if (this.txtNoiDungTraLoi.Text.Trim() != oldAnswerContent)
                        {
                            item.AnwserBy = PortalSecurity.CurrentUser.Identity.Email;
                            item.AnwserDate = DateTime.Now;
                            item.AnwserContent = this.txtNoiDungTraLoi.Text;
                            item.isAnswer = isAnswer.isAnwser;
                        }
                    }
                    else
                        item.isAnswer = isAnswer.NotAnwser;
                    item.Status = Convert.ToInt32(cbStatus.SelectedValue);
                    if (item != null)
                    {
                        item.Update();
                    }
                }

                //string url = HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, this.ItemID, ModulePages.Item);
                //Response.Redirect(url);
                Response.Redirect(HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, this.ItemID, "ApproveItem"));
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Modules.FAQ.Components.Item item = Modules.FAQ.Components.Item.Load(this.ItemID);
                item.Delete();
                string url = HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, this.ItemID, ModulePages.Item);
                Response.Redirect(url);
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            string url = HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, this.ItemID, ModulePages.Item);
            Response.Redirect(url);
        }
    }
}